﻿#include "dingdannetworkaccessmanager.h"

const static QString DINGDAN_BASE_URL = BASE_URL+"dingdan/";
const static QString DINGDAN_SAVE_DINGDAN = DINGDAN_BASE_URL+"saveDingdan";
const static QString DINGDAN_GET_DINGSANS = DINGDAN_BASE_URL+"getDingdans";
const static QString DINGDAN_GET_DINGDANDETAILS = DINGDAN_BASE_URL+"getDingdanDetails";
const static QString DINGDAN_DELETE_DINGDAN = DINGDAN_BASE_URL+"deleteDingdan";
const static QString DINGDAN_CALCULATE_DINGDAN = DINGDAN_BASE_URL+"calculateDingdan";
const static QString DINGDAN_QUERY_DINGDAN_URL = DINGDAN_BASE_URL+"queryDingdan";
const static QString DINGDAN_QUERY_DINGDAN_DETAIL_URL = DINGDAN_BASE_URL+"queryDingdanByDetail";
const static QString DINGDAN_GET_DINGDANDETAILSBYTYPE = DINGDAN_BASE_URL+"getDingdanDetailsByType";
const static QString DINGDAN_FINANCE_SUBMIT = DINGDAN_BASE_URL+"financeSubmit";
const static QString DINGDAN_GET_HISTORY_PRODUCTS = DINGDAN_BASE_URL+"getHistoryProducts";
const static QString DINGDAN_GET_IMPORT_DINGDANS = DINGDAN_BASE_URL+"getImportDingdans";
const static QString DINGDAN_GET_IMPORT_DINGDANDETAILS = DINGDAN_BASE_URL+"getImportDingdanDetails";
DingdanNetworkAccessManager::DingdanNetworkAccessManager(QObject *parent) : BaseNetworkAccessManager(parent)
{

}

void DingdanNetworkAccessManager::saveDingdan(Dingdan &dingdan){
    QByteArray data;
    data.append("dingdan="+dingdan.toJson()+"&");
    data.append("dingdanDetail="+DingdanDetail::toJson(dingdan.details));

    doPost(DINGDAN_SAVE_DINGDAN, data);
}

void DingdanNetworkAccessManager::deleteDingdan(QString username, QString dingdanId){
    QByteArray data;
    data.append("username="+username+"&");
    data.append("dingdanId="+dingdanId);

    doPost(DINGDAN_DELETE_DINGDAN, data);
}

void DingdanNetworkAccessManager::getDingdans(
        QString username,
        Dingdan::DingdanType type,
        Dingdan::DingdanState state){
    QByteArray data;
    data.append("username="+username+"&");
    data.append("type="+QString("%1").arg(type)+"&");
    data.append("state="+QString("%1").arg(state)+"&");

    doPost(DINGDAN_GET_DINGSANS, data);
}

void DingdanNetworkAccessManager::getDingdanDetails(QString username, QString dingdanId){
    QByteArray data;
    data.append("username="+username+"&");
    data.append("dingdanId="+dingdanId);

    doPost(DINGDAN_GET_DINGDANDETAILS, data);
}

void DingdanNetworkAccessManager::calculateDingdan(Dingdan &dingdan){
    QByteArray data;
    data.append("dingdan="+dingdan.toJson()+"&");
    data.append("dingdanDetail="+DingdanDetail::toJson(dingdan.details));

    doPost(DINGDAN_CALCULATE_DINGDAN, data);
}

void DingdanNetworkAccessManager::queryDingdan(
        int type,
        QString sourceUsername,
        QString targetUsername,
        QString startDate,
        QString endDate){
    QByteArray data;
    data.append("type="+QString("%1").arg(type)+"&");
    data.append("sourceUsername="+sourceUsername+"&");
    data.append("targetUsername="+targetUsername+"&");
    data.append("startDate="+startDate+"&");
    data.append("endDate="+endDate);

    doPost(DINGDAN_QUERY_DINGDAN_URL, data);
}

void DingdanNetworkAccessManager::queryDingdanDetail(
        int type,
        QString sourceUsername,
        QString targetUsername,
        QString productId,
        QString startDate,
        QString endDate){
    QByteArray data;
    data.append("type="+QString("%1").arg(type)+"&");
    data.append("sourceUsername="+sourceUsername+"&");
    data.append("targetUsername="+targetUsername+"&");
    data.append("productId="+productId+"&");
    data.append("startDate="+startDate+"&");
    data.append("endDate="+endDate);

    doPost(DINGDAN_QUERY_DINGDAN_DETAIL_URL, data);
}

void DingdanNetworkAccessManager::getDingdanDetails(
        Dingdan::FinanceWidgetType type,
        QString sourceUsername,
        QString targetUsername){
    QByteArray data;
    data.append("type="+QString("%1").arg(type)+"&");
    data.append("sourceUsername="+sourceUsername+"&");
    data.append("targetUsername="+targetUsername);

    doPost(DINGDAN_GET_DINGDANDETAILSBYTYPE, data);
}

void DingdanNetworkAccessManager::financeSubmit(
        QString companyId,
        QString sourceUsername,
        QString targetUsername,
        QString relatedMessageId,
        int relationType,
        int financeType,
        double curPreSave,
        double preSave,
        double totalFinanceMake,
        QList<DingdanDetail*> details){
    QByteArray data;
    data.append("companyId="+companyId+"&");
    data.append("sourceUsername="+sourceUsername+"&");
    data.append("targetUsername="+targetUsername+"&");
    data.append("relatedMessageId="+relatedMessageId+"&");
    data.append("relationType="+QString("%1").arg(relationType)+"&");
    data.append("financeType="+QString("%1").arg(financeType)+"&");
    data.append("totalFinanceMake="+QString("%1").arg(totalFinanceMake)+"&");
    data.append("curPreSave="+QString("%1").arg(curPreSave)+"&");
    data.append("preSave="+QString("%1").arg(preSave)+"&");
    data.append("details="+DingdanDetail::toJson(details));

    doPost(DINGDAN_FINANCE_SUBMIT, data);
}

void DingdanNetworkAccessManager::getHistoryProducts(
        QString sourceCompanyId,
        QString targetCompanyId,
        QString sourceProductId,
        int type){
    QByteArray data;
    data.append("sourceCompanyId="+sourceCompanyId+"&");
    data.append("targetCompanyId="+targetCompanyId+"&");
    data.append("sourceProductId="+sourceProductId+"&");
    data.append("type="+QString("%1").arg(type));

    doPost(DINGDAN_GET_HISTORY_PRODUCTS, data);
}

void DingdanNetworkAccessManager::getImportDingdans(
        QString username,
        Dingdan::DingdanState state){
    QByteArray data;

    data.append("username="+username+"&");
    data.append("state="+QString("%1").arg(state));

    doPost(DINGDAN_GET_IMPORT_DINGDANS, data);
}

void DingdanNetworkAccessManager::getImportDingdanDetails(
        QString sourceUsername,
        QString targetUsername,
        QString sourceCompanyId,
        QString targetCompanyId,
        QString dingdanId,
        int type){
    QByteArray data;

    data.append("sourceUsername="+sourceUsername+"&");
    data.append("targetUsername="+targetUsername+"&");
    data.append("sourceCompanyId="+sourceCompanyId+"&");
    data.append("targetCompanyId="+targetCompanyId+"&");
    data.append("dingdanId="+dingdanId+"&");
    data.append("type="+QString("%1").arg(type));

    doPost(DINGDAN_GET_IMPORT_DINGDANDETAILS, data);
}
